"""
题目：计算二叉树中叶子节点（左右子节点都为空的节点）的总数。
"""


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def count_leaf_nodes(root):
    """计算二叉树中叶子节点的总数（递归实现）"""
    # 空节点没有叶子节点，返回0
    if not root:
        return 0

    # 左右子节点都为空，说明是叶子节点，返回1
    if not root.left and not root.right:
        return 1

    # 否则，叶子节点总数 = 左子树叶子节点数 + 右子树叶子节点数
    return count_leaf_nodes(root.left) + count_leaf_nodes(root.right)


# 辅助函数：创建二叉树（复用）
def create_binary_tree(arr):
    if not arr:
        return None
    root = TreeNode(arr[0])
    queue = [root]
    index = 1
    while queue and index < len(arr):
        current = queue.pop(0)
        if arr[index] is not None:
            current.left = TreeNode(arr[index])
            queue.append(current.left)
        index += 1
        if index < len(arr) and arr[index] is not None:
            current.right = TreeNode(arr[index])
            queue.append(current.right)
        index += 1
    return root


# 测试
root = create_binary_tree([1, 2, 3, 4, None, 5, 6])
# 树结构：
#       1
#      / \
#     2   3
#    /   / \
#   4   5   6
# 叶子节点是：4、5、6 → 共3个
print(count_leaf_nodes(root))  # 输出: 3
